package com.itheima.Service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Service.SetmealService;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckgroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CheckgroupMapper checkgroupMapper;

    /**
     * 新增体验套餐
     * @param setmeal
     * @param checkgroupIds
     */
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealMapper.add(setmeal);
        Integer setmealId = setmeal.getId();
        List<Map<String, Integer>> maps = new ArrayList<>();
        if (checkgroupIds!= null && checkgroupIds.length > 0){
            for (Integer checkgroupId : checkgroupIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("setmeal_id", setmealId);
                map.put("checkgroup_id", checkgroupId);
                maps.add(map);
            }
            setmealMapper.setSetmealAndCheckGroup(maps);
        }
    }


    /**
     * 编辑套餐
     * @param setmeal
     * @param checkGroupIds
     */
    public void edit(Setmeal setmeal, Integer[] checkGroupIds) {
        Integer setmealId = setmeal.getId();
        setmealMapper.deleteSetmealAndCheckGroup(setmealId);

        if(checkGroupIds != null && checkGroupIds.length > 0){
            List<Map<String, Integer>> maps = new ArrayList<>();
            for (Integer checkGoupId : checkGroupIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("setmeal_id",setmealId);
                map.put("checkgroup_id",checkGoupId);
                maps.add(map);
            }
            setmealMapper.setSetmealAndCheckGroup(maps);
        }
        setmealMapper.edit(setmeal);
    }



    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public Setmeal findById(String id) {
        Setmeal setmeal = setmealMapper.findById(id);
        return setmeal;
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        int pageNum = queryPageBean.getCurrentPage();
        int pageSize = queryPageBean.getPageSize();

        PageHelper.startPage(pageNum,pageSize);

        Page<Setmeal> page = setmealMapper.findPage(queryPageBean);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 查询套餐占比
     * @return
     */
    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setmealMapper.findSetmealCount();
    }
}
